// pages/sjpingjia/sjpingjia.js
var app = getApp();
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    papersid:0,
    url:'',
    getpaperinfo: app.data.url +'/zhongxue/maoapi/getpaperinfo',
    addhuifu: app.data.url + '/zhongxue/maoapi/addhuifu',
    values:[],

    shuju: {},
    kong:true,

    huihua: app.data.url +"/uploads/zhongxue/huihuas.png",
    pinglun: []
  
  },
    
  fangda:function(){
  wx.previewImage({
    current: this.data.shuju.testpaper[0],
    urls: this.data.shuju.testpaper,
  })
  },
  xiazais:function(e){
    wx.showModal({
      title: '提示',
      content: '请在电脑端下载资料,官方网址：' + this.data.url,
      showCancel: false,
      // cancelText: '',
      // cancelColor: '',
      confirmText: '确定',
      // confirmColor: '',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
    // var id = e.currentTarget.dataset.id;
    // wx.showLoading({
    //   title: '下载中',
    // })
    // wx.downloadFile({
    //   url: app.data.url +'/zhongxue/pdfs/index?id='+id,
    //   header: {},
    //   success: function(res) {
    //     console.log(res);
    //     var tempFilePath = res.tempFilePath;
    //     wx.saveFile({
    //       tempFilePath: tempFilePath,
    //       success:function(ss){
    //         console.log(ss);
    //         var savedFilePath = ss.savedFilePath;
    //         wx.openDocument({
    //           filePath: savedFilePath,
    //           fileType: '',
    //           success: function(){
    //             wx.hideLoading();
    //             wx.showToast({
    //               title: '已下载到' + savedFilePath,
    //               icon: 'none',
    //               duration:5000,
    //             })
    //           },
    //         })
    //       }
    //     })
    //   },
    //   fail: function(res) {},
    //   complete: function(res) {},
    // })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    this.setData({papersid:id});
    this.getData()
  },
  getData:function(){
    var id = this.data.papersid;
    var user = wx.getStorageSync('user');
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.getpaperinfo,
      data: {token:user.token,id:id},
      header: {},
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        var data = res.data;
        console.log(data);
        if(data.code==200){
          var shuju = data.data.data;
          var url = data.data.url;
          that.setData({url:url})
          var pinglun = data.data.pinglun;
          that.setData({shuju:shuju,pinglun:pinglun});
          console.log(shuju)
          console.log(pinglun)
          var values = [];
          ea.each(pinglun,function(){
            values.push("");
          })
          that.setData({ values: values});
        }else{
          wx.showToast({
            title: data.msg,
            icon:'none',
            duration: app.data.quanbushijian
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {
        wx.hideLoading()
      },
    })
  },
  fasong:function(e){
// console.log(e);
    var kong = this.data.kong;
    if(!kong){
      return;
    }
    this.setData({kong:true})
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var values = this.data.values;
    var str = values[index];
    if(!str){
      wx.showToast({
        title: '请输入回复内容',
        icon:'none',
        duration: app.data.quanbushijian
      })
      this.setData({kong:true})
      return
    }
    var user = wx.getStorageSync('user');
    // console.log(str);
    var that = this;
    wx.showLoading({
      title: '回复中',
    })
    wx.request({
      url: that.data.addhuifu,
      data: { token: user.token, pingjia_id: id, str: str},
      header: {},
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        var data = res.data;
        console.log(data);
        if(data.code==200){
          var pinglun = that.data.pinglun;
          var huifu = pinglun[index].huifu;
          huifu.push({
            dir:str,
            name:user.user
          });
          pinglun[index].huifu = huifu;
          that.setData({pinglun:pinglun});
          var values = that.data.values;
          values[index] = "";
          that.setData({values:values});
          wx.showToast({
            title: '回复成功',
            duration: app.data.quanbushijian
          })
        }else{
          wx.showToast({
            title: data.msg,
            icon:'none',
            duration: app.data.quanbushijian
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {
        that.setData({kong:true})
        wx.hideLoading();
      },
    })
  },
  tianjiashujus:function(e){
    var value = e.detail.value;
    var values = this.data.values;
    var index = e.currentTarget.dataset.index;
    values[index] = value;
    // console.log(values)
    this.setData({values:values});
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      this.getData();
      wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/pages/indexfirst/indexfirst',
    }
  }
})